﻿Imports System.ComponentModel.Composition
Imports System.Collections.ObjectModel
Imports Pronama.NamaAlert.AddIn.ViewModel

<AddInExport("{52A5365B-C710-4C20-AD50-48DCA72EDF5D}", "Alert Sidebar",
    Description:="",
    HasSetting:=False,
    HasContent:=False,
    HasSidebar:=True)>
<Export(GetType(IDestinationAddIn))>
Public Class AlertSidebar
    Implements IDestinationAddIn

    Private Activated As Boolean
    Private WithEvents ViewModel As New AlertSidebarViewModel

    Private Settings As ISettings
    Private Helper As IHelper


#Region "Implementation"

    <Import(GetType(PostMessageCallback))>
    Property PostMessage As PostMessageCallback Implements IDestinationAddIn.PostMessage

    Public Event Deactivated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAddIn.Deactivated
    Protected Sub OnDeactivated(ByVal e As EventArgs)
        RaiseEvent Deactivated(Me, e)
    End Sub

    Public Sub Initialize(ByVal settings As ISettings, ByVal helper As IHelper) Implements IAddIn.Initialize
        Me.Settings = settings
        Me.Helper = helper
        AlertItemViewModel.Helper = Me.Helper
    End Sub

    Public Sub Activate() Implements IAddIn.Activate
        Activated = True

        'Dim r = New Random
        'For i = 0 To 30
        '    Dim item = New AlertItemViewModel
        '    item.Title = "あああああああ"
        '    item.Description = New String("い"c, r.Next(3, 10))
        '    item.IconUri = New Uri("http://icon.nimg.jp/community/co9320.jpg?1292057947")

        '    ViewModel.AlertItems.Add(item)
        'Next


    End Sub

    Public Sub Deactivate() Implements IAddIn.Deactivate
        Activated = False
    End Sub

    Public Function GetContentControl(ByVal message As Message) As System.Windows.Controls.Control Implements IAddIn.GetContentControl
        Throw New NotSupportedException
    End Function

    Public Function GetSettingControl() As System.Windows.Controls.Control Implements IAddIn.GetSettingControl
        Throw New NotSupportedException
    End Function

    Public Function GetSidebarControl() As System.Windows.Controls.Control Implements IAddIn.GetSidebarControl

        Dim sidebar = New AlertSidebarPage
        sidebar.DataContext = ViewModel
        Return sidebar

    End Function

    Public Sub ReceiveMessage(ByVal sourceAddIn As IAddIn, ByVal message As Message) Implements IDestinationAddIn.ReceiveMessage
        If message.LiveProgram Is Nothing Then
            Exit Sub
        End If

        Dim i = New AlertItemViewModel

        With message.LiveProgram

            i.Title = .Title
            i.Description = .Description
            i.Uri = .Uri

            If .ChannelCommunity IsNot Nothing Then
                i.StationName = .ChannelCommunity.Name
                i.IconUri = .ChannelCommunity.IconUri
                i.StationUri = .ChannelCommunity.Uri
            End If
        End With


        Me.Helper.Dispatcher.BeginInvoke(
            Sub()
                ViewModel.AlertItems.Insert(0, i)
            End Sub)

    End Sub

    Public Sub WriteLog(ByVal sourceAddIn As IAddIn, ByVal log As Log) Implements IDestinationAddIn.WriteLog

    End Sub

#End Region

    Private Function GetMetadata(ByVal sourceAddIn As IAddIn) As AddInExportAttribute
        Dim attr = sourceAddIn.GetType.GetCustomAttributes(GetType(AddInExportAttribute), True)
        If attr.Count = 0 Then
            Return Nothing
        End If
        Return DirectCast(attr.First, AddInExportAttribute)
    End Function



End Class
